package com.epalmpay.service.datareport.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.epalmpay.dto.datareport.GroupAccountLogDayOutDTO;
import com.epalmpay.dto.manage.datareport.TradeDetailInDTO;
import com.epalmpay.dto.manage.datareport.TradeDetailOutDTO;
import com.epalmpay.dto.manage.datareport.TradeStatisShopDayOutDTO;
import com.epalmpay.entity.Page;
import com.epalmpay.entity.StatisShopMonth;
import com.epalmpay.mapper.StatisShopDayMapper;
import com.epalmpay.mapper.StatisShopMonthMapper;
import com.epalmpay.service.datareport.ITradeDetailService;

@Service("tradeDetailService")
public class TradeDetailServiceImpl implements ITradeDetailService{
	
	@Resource
	private StatisShopMonthMapper statisShopMonthMapper;
	
	@Resource
	private StatisShopDayMapper statisShopDayMapper;

	@Override
	public List<TradeDetailOutDTO> queryTradeDetailList(TradeDetailInDTO tradeDetailInDTO, Page page) {
		
		Map<String,Object> param=new HashMap<String,Object>();
		param.put("groupName", tradeDetailInDTO.getGroupName());
		param.put("statisDate",tradeDetailInDTO.getStringStatisDate());
		
		
		List<TradeDetailOutDTO> result = new ArrayList<TradeDetailOutDTO>();

		int sumCount = statisShopMonthMapper.selectTradeDetailCount(param);
		page.setTotalResult(sumCount);
		if(sumCount<=0)
			return null;
		
		param.put("limit", page.getShowCount());
		param.put("start", page.getShowCount()*(page.getCurrentPage()-1));
		
		List<Map<String,Object>> tradeDetailList = statisShopMonthMapper.selectTradeDetailList(param);
		
		for (Map<String, Object> tradeDetail : tradeDetailList) {
			TradeDetailOutDTO tradeDetailOutDTO= new TradeDetailOutDTO();
			tradeDetailOutDTO.setId((Long)(tradeDetail.get("id")));
			tradeDetailOutDTO.setShopNo((Long)(tradeDetail.get("shop_no")));
			tradeDetailOutDTO.setShopName((String)tradeDetail.get("shop_name"));
			tradeDetailOutDTO.setGroupName((String)tradeDetail.get("group_name"));
			tradeDetailOutDTO.setThisAmount((Integer)tradeDetail.get("this_amount"));
			tradeDetailOutDTO.setThisOrderNum((Integer)tradeDetail.get("this_order_num"));
			tradeDetailOutDTO.setStatisDate((String)tradeDetail.get("statis_date"));
			
			result.add(tradeDetailOutDTO);
		}
		
		return result;
		
	}

	@Override
	public StatisShopMonth queryStatisShopMonthById(Long id) throws Exception {
		return statisShopMonthMapper.queryStatisShopMonthById(id);
	}

	@Override
	public List<TradeStatisShopDayOutDTO> queryStatisShopDayList(TradeDetailInDTO tradeDetailInDTO, Page page) {
		Map<String,Object> param=new HashMap<String,Object>();
		param.put("statisDate", tradeDetailInDTO.getStringStatisDate()+0+0);
		param.put("shopId", tradeDetailInDTO.getShopId());
		
		List<TradeStatisShopDayOutDTO> result = new ArrayList<TradeStatisShopDayOutDTO>();

		int sumCount = statisShopDayMapper.queryTradeStatisShopDayCount(param);
		page.setTotalResult(sumCount);
		if(sumCount<=0)
			return null;
		
		param.put("limit", page.getShowCount());
		param.put("start", page.getShowCount()*(page.getCurrentPage()-1));
		
		List<Map<String,Object>>  statisShopDayList = statisShopDayMapper.queryTradeStatisShopDayList(param);
		
		
		for (Map<String, Object> statisShopDay : statisShopDayList) {
			TradeStatisShopDayOutDTO statisShopDayOutDTO = new TradeStatisShopDayOutDTO();
			statisShopDayOutDTO.setId((Long)statisShopDay.get("id"));
			statisShopDayOutDTO.setShopNo((Long)statisShopDay.get("shop_no"));
			statisShopDayOutDTO.setStatisDate((Date)statisShopDay.get("statis_date"));
			statisShopDayOutDTO.setConsumeReceivableAmount(Integer.parseInt(statisShopDay.get("consume_receivable_amount").toString()));
			statisShopDayOutDTO.setFactAmount(Integer.parseInt(statisShopDay.get("fact_amount").toString()));
			statisShopDayOutDTO.setProfitReceivableAmount(Integer.parseInt(statisShopDay.get("profit_receivable_amount").toString()));
			statisShopDayOutDTO.setOrderNum(Integer.parseInt(statisShopDay.get("order_num").toString()));
			result.add(statisShopDayOutDTO);
		}
		
		return result;
	}

}
